<?php
// +----------------------------------------------------------------------
// |我的地址
// +----------------------------------------------------------------------
// |Data:2016-06-25 18:45
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Wap\Controller;
use Common\Controller\CommonController;

class AddressController extends CommonController{
    private $_model;
    protected function _initialize(){
        parent::_initialize();
        $this->_model = D('Wap/Address');
    }

    /**
     * 列表
     */
    public function index(){
        $map = array();
        $map['uid'] = $this->uid;
        $list = $this->_model->getAll($map,'add_time desc');
        $this->assign('list',$list);
        $this->display();
    }

    public function edit(){
        $data = I('get.');
        if($data['id']){
            $detail = $this->_model->detail($data['id']);
            $this->assign('detail',$detail);
        }
        $this->assign('data',$data);
        $this->display();
    }

    public function update(){
        if(IS_POST){
            $data = $this->checkPostData();
            empty($data) || !is_array($data) && $this->returnData('请填写表单数据！');
            $msg = $data['id'] ? '编辑' : '添加';
            $result = $this->_model->update($data);
            if($result){
                $this->returnData($msg.'成功！',1,$result);
            }else{
                $this->returnData($msg.'失败！');
            }
        }else{
            $this->returnData('提交方式错误！');
        }
    }

    public function del(){
        $id = I('get.id');
        empty($id) && $this->returnData('请选择操作项！');
        $result = $this->_model->del($id,$this->uid);
        if($result){
            $this->returnData('删除成功！',1);
        }else{
            $this->returnData('删除失败！');
        }
    }

    /**
     * 地图页面
     */
    public function map(){
        $this->display();
    }

    /**
     * 检测表单数据
     * @return array
     */
    private function checkPostData(){
        $id = I('post.id');
        $contact = I('post.contact');
        $mobile = I('post.mobile');
        $location = I('post.location');
        $main_address = I('post.main_address');
        $sub_address = I('post.sub_address');

        empty($contact) && $this->returnData('请输入联系人！');
        if(abslength($contact) > 30) $this->returnData('联系人长度1~30个字符！');
        empty($mobile) && $this->returnData('请输入联系方式！');
        if(!mobileRegular($mobile)) $this->returnData('联系方式格式错误！');
        if(empty($location) || empty($main_address)) $this->returnData('请选择地址！');
        if($sub_address && abslength($sub_address) > 200) $this->returnData('详细地址长度1~200个字符！');

        $data = array(
            'id'=>$id,
            'uid'=>$this->uid,
            'contact'=>$contact,
            'mobile'=>$mobile,
            'location'=>$location,
            'main_address'=>$main_address,
            'sub_address'=>$sub_address,
        );
        if($id){
            $data['id'] = $id;
            $data['edit_time'] = time();
        }else{
            $data['add_time'] = time();
        }
        return $data;
    }
}
?>